package com.hzsai.dynamicdatasource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.lang.Nullable;

/**
 * hzsai 2019/8/27
 * usage:
 **/
public class DynamicDataSourceRouting extends AbstractRoutingDataSource {

    private static final Logger logger = LoggerFactory.getLogger(DynamicDataSourceRouting.class);

    @Nullable
    @Override
    protected Object determineCurrentLookupKey() {
        logger.info("当前数据库是: " + DataSourceContextHolder.getDB());

        return DataSourceContextHolder.getDB();
    }
}
